<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class OrderOutbound extends Model
{
    protected $table = 'ims_yz_order_outbound';
    public $timestamps = false;
    
    protected $fillable = [
        'uniacid',
        'outbound_sn',
        'order_sn',
        'vendor_id',
        'finish_time',
        'send_time',
        'cancel_time',
        'status',
        'dispatch_type_id',
        'order_id',
        'deliver_id',
        'express_code',
        'express_sn',
        'express_name',
        'created_at',
        'updated_at'
    ];

    protected $casts = [
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'finish_time' => 'integer',
        'send_time' => 'integer',
        'cancel_time' => 'integer'
    ];

    // 状态常量
    const STATUS_CLOSED = -1;    // 已关闭
    const STATUS_UNPAID = 0;     // 未支付
    const STATUS_PENDING_SEND = 1; // 待发货
    const STATUS_PENDING_RECEIVE = 2; // 待收货
    const STATUS_COMPLETED = 3;  // 已完成

    /**
     * 关联订单
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }

    /**
     * 关联发货单商品
     */
    public function outboundGoods()
    {
        return $this->hasMany(OrderOutboundGoods::class, 'outbound_id', 'id');
    }

    /**
     * 获取状态文本
     */
    public function getStatusTextAttribute()
    {
        $statusMap = [
            self::STATUS_CLOSED => '已关闭',
            self::STATUS_UNPAID => '未支付',
            self::STATUS_PENDING_SEND => '待发货',
            self::STATUS_PENDING_RECEIVE => '待收货',
            self::STATUS_COMPLETED => '已完成'
        ];

        return $statusMap[$this->status] ?? '未知状态';
    }
}